﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// WMS条码均值数据更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WMSBarcodeJZController : ControllerBase
    {
        private readonly ILogger<BXCustomerInfoRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con2;


        public WMSBarcodeJZController(ILogger<BXCustomerInfoRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection_ZH");
            con2 = _configuration.GetConnectionString("ZH_MES_DbConnection");

        }

        /// <summary>
        /// WMS条码均值数据更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWMSBarcodeJZ([FromBody] BXCustomerInfoRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            BXCustomerInfoResponseDto re = new BXCustomerInfoResponseDto();
            string ddate = strparm.ddate;

            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {

                strSql = new StringBuilder();
                strSql.Append(@"select * from barcode_file where warehouse_code in ('JWZGP01','JWZGP02','JWZGP03','JWZGP04') and
material_code like '14%' and barcode_status=4 and barcode_type=40 and material_name like '%A+%'");
                DataTable dt_wmstkc = DB.Query(strSql.ToString(), con);
                if (dt_wmstkc.Rows.Count>0)
                {
                    foreach (DataRow item in dt_wmstkc.Rows)
                    {
                        string barcode_no = item["barcode_no"].ToString();
                        strSql = new StringBuilder();
                        strSql.Append(@"select * from (select 
  a.CUPACKINGTIME,
  a.cuassociatepackingname as  托号, 
  a.description as 托备注,
 p.description as 物料描述 ,
c.dz as 托电阻均值,
   round(cast(replace(c.f1,'a',0) as int)/hd.sl,3) as 点5比例,
c.hd as 厚度均值
from cuAssociatePacking a 
inner join product p on a.cuproductid=p.productid
inner join productbase pb on p.productbaseid=pb.productbaseid
left join cu_mes_trayavg_N c on a.cuassociatepackingname=c.trayno 
left join (select trayno,sum(SL) sl from cu_mes_trayspecial_hd where dzfb is not null GROUP BY trayno) hd on a.cuassociatepackingname = hd.trayno
where a.cupackingtype='Pallet'  and  c.dz is not null

union 

select 
  a.CUPACKINGTIME,
  a.cuassociatepackingname as  托号, 
  a.description as 托备注,
 p.description as 物料描述 ,
c.dz as 托电阻均值,
   round(cast(replace(c.dzsl,'a',0) as int)/hd.sl,3) as 点5比例,
c.hd as 厚度均值
from cuAssociatePacking a 
inner join product p on a.cuproductid=p.productid
inner join productbase pb on p.productbaseid=pb.productbaseid
left join cu_mes_trayavg_N1 c on a.cuassociatepackingname=c.trayno 
left join (select trayno,sum(SL) sl from cu_mes_trayspecial_hd where dzfb is not null GROUP BY trayno) hd on a.cuassociatepackingname = hd.trayno
where a.cupackingtype='Pallet'  and  c.dz is not null) sour where 托号='"+ barcode_no + "'");
                        DataTable dt_mes_jz = DB.OracleQuery(strSql.ToString(), con2);
                        if (dt_mes_jz.Rows.Count > 0)
                        {
                            string hdjz = "";
                            if (dt_mes_jz.Rows[0]["厚度均值"].ToString()=="")
                            {
                                hdjz = "0";
                            }
                            else
                            {
                                hdjz = dt_mes_jz.Rows[0]["厚度均值"].ToString();
                            }

                            string dzjz = "";
                            if (dt_mes_jz.Rows[0]["托电阻均值"].ToString() == "")
                            {
                                dzjz = "0";
                            }
                            else
                            {
                                dzjz = dt_mes_jz.Rows[0]["托电阻均值"].ToString();
                            }

                            string jzjz = "";
                            if (dt_mes_jz.Rows[0]["点5比例"].ToString() == "")
                            {
                                jzjz = "0";
                            }
                            else
                            {
                                jzjz = dt_mes_jz.Rows[0]["点5比例"].ToString();
                            }


                            strSql = new StringBuilder();
                            strSql.Append(@"update barcode_file set average_thickness="+ hdjz + ",average_resistance="+ dzjz + ",average_resistancefocus="+ jzjz + " where barcode_no='"+ barcode_no + "'");
                            strSqlList.Add(strSql.ToString());
                        }
                        
                    }

                    if (strSqlList.Count>0)
                    {
                        bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                        if (A == false)
                        {
                            strMsg = "数据库更新失败，请稍后重试";
                        }
                    }
                   
                }



               
          
                  //re.data = DB.TableToListModel<BXCustomerInfoResponseData>(dt_customer);
                





            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "0";
            }
            else
            {
                re.status = "失败";
                re.code = "500";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }

      
    }
}
